package lgq.xuan.httprequest.phone.manager;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import lgq.xuan.httprequest.phone.manager.base.BasePermissionManager;

public class CalledPermissionManager extends BasePermissionManager {

    private final int CALLED_PERMISSION_CODE = 101010010;

    public CalledPermissionManager(@NonNull Activity activity) {
        super(activity);
    }

    /**
     * 判断权限
     *
     * @return 是否拥有足够权限
     */
    @Override
    public boolean checkAllNeedPermission() {
        return checkManageOwnCalls() &&
                checkReadCallLog() &&
                checkReadPhoneNumbers() &&
                checkReadPhoneState() &&
                checkCallPhone();
    }

    private boolean checkManageOwnCalls() {
        int permission = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            permission = ContextCompat.checkSelfPermission(activity, Manifest.permission.MANAGE_OWN_CALLS);
        }
        return permission == PackageManager.PERMISSION_GRANTED;
    }

    private boolean checkReadCallLog() {
        int permission = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CALL_LOG);
        return permission == PackageManager.PERMISSION_GRANTED;
    }

    private boolean checkReadPhoneState() {
        int permission = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);
        return permission == PackageManager.PERMISSION_GRANTED;
    }

    private boolean checkReadPhoneNumbers() {
        int permission = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            permission = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_NUMBERS);
        }
        return permission == PackageManager.PERMISSION_GRANTED;
    }

    private boolean checkCallPhone() {
        int permission = ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE);
        return permission == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 请求权限
     */
    @Override
    public void requestAllNeedPermission() {

        String[] permissions;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            permissions = new String[]{
                    Manifest.permission.MANAGE_OWN_CALLS,
                    Manifest.permission.READ_CALL_LOG,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_PHONE_NUMBERS,
                    Manifest.permission.CALL_PHONE,
            };
        } else {
            permissions = new String[]{
                    Manifest.permission.READ_CALL_LOG,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.CALL_PHONE,
            };
        }

        ActivityCompat.requestPermissions(
                activity,
                permissions,
                CALLED_PERMISSION_CODE
        );

    }

}
